﻿@{
    Layout = null;
}
@*<link href="/content/css/common.css?v=@app.PlatformSettings.VersionNumber" rel="stylesheet" />
    <link href="/content/css/bootstrap.min.css?v=@app.PlatformSettings.VersionNumber" rel="stylesheet" />
    <input data-val="true" data-val-required="SolutionId 字段是必需的。" id="SolutionId" name="solutionid" type="hidden" value="0d3754d8-05b9-41ce-9ae0-83125fd79c27">
    <input data-val="true" data-val-required="EntityId 字段是必需的。" id="EntityId" name="entityid" type="hidden" value="6de3a9f9-fea5-4024-8a5a-461dd7916fdf">
    <input data-val="true" data-val-required="EntityId 字段是必需的。" id="RelevantEntityId" name="entityid" type="hidden" value="6de3a9f9-fea5-4024-8a5a-461dd7916fdf">
    <input id="RelevantEntityInfo_Name" name="relevantentityinfo.name" type="hidden" value="SalesOrder">
    <input id="EntityInfo_Name" name="entityinfo.name" type="hidden" value="SalesOrder">*@
<link href="/content/js/bootstrap-datepicker-1.5.0/css/bootstrap-datepicker3.min.css?v=@app.PlatformSettings.VersionNumber" rel="stylesheet">
<div class="modal fade" data-backdrop="static" id="PilotModal" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" onclick="CancelPilot()"
                        aria-hidden="true">
                    @app.T["times_sign"]<!--×-->
                </button>
                <h4 class="modal-title" id="PilotModalLabel">
                    @app.T["report_wizard"]<!--报表向导-->
                </h4>
            </div>
            @*<div class="modal-body" id="pliot-page1" data-page="1">
                    <div class="form-group">
                        <label for="pilot-newreport">主要记录筛选条件</label>
                    </div>
                    <div class="form-group">
                        <div class="btn-group btn-group-sm">
                            <button type="button" onclick="clearRowGroup(this)" class="btn btn-default">清除</button>
                            <button type="button" onclick="groupAndOr(this,'and')" class="btn btn-default">组“和”</button>
                            <button type="button" onclick="groupAndOr(this,'or')" class="btn btn-default">组“或”</button>
                        </div>
                    </div>
                    <div class="form-group" data-type="Main">
                        <div id="row_1" class="pilot-row row" data-isshow="show" style="margin-bottom:5px;">
                            <div class="col-sm-1" style="position:relative;height:30px;">
                                <em class="pilot-choose" onclick="downSelect(this,'row')" style="display: none; background: url(../content/images/report/selectListR.png) center no-repeat; position: absolute; height: 15px; width: 15px; top: 30%; left: 50%; cursor: pointer;"></em>
                            </div>
                            <div class="col-sm-11">
                                <select class="form-control pilot-fieldM pilot-null input-sm">
                                    <option value="">--请选择--</option>
                                </select>
                            </div>
                            <div class="col-sm-3 hide">
                                <select class="form-control pilot-condition input-sm">
                                    <option>--请选择--</option>
                                </select>
                            </div>
                            <div class="col-sm-4 hide">
                                <input class="form-control input-sm" name="value" type="text" value="" />
                            </div>
                            <br />
                        </div>
                        <div class="clearfix"></div>
                    </div>
                    <div class="form-group">
                        <label for="pilot-oldreport">相关记录筛选条件</label>
                    </div>
                    <div class="form-group">
                        <div class="btn-group btn-group-sm">
                            <button type="button" onclick="clearRowGroup(this)" class="btn btn-default">清除</button>
                            <button type="button" onclick="groupAndOr(this,'and')" class="btn btn-default">组“和”</button>
                            <button type="button" onclick="groupAndOr(this,'or')" class="btn btn-default">组“或”</button>
                        </div>
                    </div>
                    <div class="form-group" data-type="Relevant">
                        <div id="row_2" class=" pilot-row row" data-isshow="show">
                            <div class="col-sm-1" style="position:relative;height:30px;">
                                <em class="pilot-choose" onclick="downSelect(this,'row')" style="display: none; background: url(../content/imgs/selectListR.png) center no-repeat; position: absolute; height: 15px; width: 15px; top: 30%; left: 50%; cursor: pointer;"></em>
                            </div>
                            <div class="col-sm-11">
                                <select class="form-control pilot-fieldR pilot-null input-sm">
                                    <option value="">--请选择--</option>
                                </select>
                            </div>
                            <div class="col-sm-3 hide">
                                <select class="form-control pilot-condition input-sm">
                                    <option>--请选择--</option>
                                </select>
                            </div>
                            <div class="col-sm-4 hide">
                                <input class="form-control input-sm" name="value" type="text" value="" />
                            </div>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                    <div class="clearfix"></div>
                </div>*@
            <div id="pliot-page2" class="modal-body" data-page="2" style="display:none;">
                <div class="form-group">
                    <label for="pilot-newreport">@app.T["select_report_line_group_amount"]</label><!--选择报表中所要显示的列、分组以及总计。-->
                </div>
                <div class="form-group">
                    <div class="btn-group btn-group-sm">
                        <button type="button" onclick="DelGroupColumn('group')" class="btn btn-default"><span class="glyphicon glyphicon-trash"></span> @app.T["delete_grouping"]</button><!--删除分组-->
                        <button type="button" onclick="EditGroupColumu('group')" class="btn btn-default"><span class="glyphicon glyphicon-edit"></span> @app.T["change_attribute"]</button><!--更改属性-->
                        <button type="button" onclick="FieldUpDown('up')" class="btn btn-default"><span class="glyphicon glyphicon-arrow-up"></span></button>
                        <button type="button" onclick="FieldUpDown('down')" class="btn btn-default"><span class="glyphicon glyphicon-arrow-down"></span></button>
                    </div>
                </div>

                <div id="GroupList" class="form-group">
                    <div class="group-row row">
                        <div class="col-sm-1 placeholder"></div>
                        <div class="col-sm-4 group-text group-null" style="border: 2px dashed #ccc; cursor: pointer;">
                            <span>@app.T["click_add_group"]</span><!--单击此处添加分组-->
                        </div>
                    </div>
                    <div class="group-row row">
                        <div class="col-sm-2 placeholder"></div>
                        <div class="col-sm-4 group-text group-null" style="border: 2px dashed #ccc; cursor: pointer;">
                            &nbsp;
                        </div>
                    </div>
                    <div class="group-row row">
                        <div class="col-sm-3 placeholder"></div>
                        <div class="col-sm-4 group-text group-null" style="border: 2px dashed #ccc; cursor: pointer;">
                            &nbsp;
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="btn-group btn-group-sm">
                        <button type="button" onclick="OpenPilotSort()" class="btn btn-default"><span class="glyphicon glyphicon-sort"></span> @app.T["configure_sorting"]</button><!--配置排序-->
                        <button type="button" onclick="DelGroupColumn('column')" class="btn btn-default"><span class="glyphicon glyphicon-trash"></span> @app.T["delete_line"]</button><!--删除列-->
                        <button type="button" onclick="EditGroupColumu('column')" class="btn btn-default"><span class="glyphicon glyphicon-edit"></span> @app.T["change_attribute"]</button><!--更改属性-->
                        <button type="button" onclick="OpenSetRow()" class="btn btn-default"><span class="glyphicon glyphicon-sort-by-alphabet"></span> @app.T["set_front_behind_line"]</button><!--设置前/后行数-->
                        <button type="button" onclick="FieldLeftRight('left')" class="btn btn-default "><span class="glyphicon glyphicon-arrow-left"></span></button>
                        <button type="button" onclick="FieldLeftRight('right')" class="btn btn-default"><span class="glyphicon glyphicon-arrow-right"></span></button>
                    </div>
                </div>
                <div class="form-group">
                    <div class="table-responsive">
                        <table class="table table-bordered col-sm-12">
                            <thead>
                                <tr id="ColumnField" class="warning "><th class="AddBtn" style="cursor:pointer;" onclick="openAddField('column')">@app.T["click_add_columns"]</th></tr><!--单击此处添加列-->
                            </thead>
                            <tbody>
                                <tr class="info">
                                    <td>@app.T["report_data"]</td>
                                </tr>
                                <tr class="info">
                                    <td>@app.T["report_data"]</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <div id="pliot-page3" class="modal-body" data-page="3" style="display:none;">
                <div class="form-group">
                    <label for="pilot-newreport">@app.T["set_report_form"]</label><!--设置报表格式-->
                    <span id="page3-tips">@app.T["report_tips"]</span>
                </div>
                <div class="form-group">
                    <input id="OnlyTable" name="PilotFormat" type="radio" value="Normal" class="inline" checked="checked" />
                    <label for="OnlyTable">@app.T["only_show_form"]</label>
                    <img src="/content/images/report/onlytable.png" />
                </div>
                <div class="form-group">
                    <input id="TableChart" name="PilotFormat" type="radio" value="Normal" class="inline" />
                    <label for="TableChart">@app.T["togetherpage_show_form_"]</label><!--在同一页的图表下方显示表格：-->
                    <img src="/content/images/report/tableinchart.png" />
                </div>
                <div class="form-group">
                    <input id="TableInChart" name="PilotFormat" type="radio" value="Drillthrough" class="inline" />
                    <label for="TableInChart">@app.T["show_chart_explain"]</label><!--显示图表。若要查看图表区域的数据，请单击图表区域：-->
                    <img src="/content/images/report/tablechart.png" />
                </div>
            </div>
            <div id="pliot-page4" class="modal-body" data-page="4" style="display:none;">
                <div class="form-group">
                    <label>@app.T["select_chart_type"]</label><!--选择图表类型-->
                </div>
                <div class="form-group">
                    <input id="PiotXChart" name="PilotChartType" type="radio" value="Column" class="inline" checked="checked" />
                    <label for="PiotXChart">@app.T["vertical_bar_chart"]</label><!--垂直条形图：-->
                    <img src="/content/images/report/XChart.png" />
                </div>
                <div class="form-group">
                    <input id="PiotYChart" name="PilotChartType" type="radio" value="Bar" class="inline" />
                    <label for="PiotYChart">@app.T["level_bar_chart"]</label><!--水平条形图：-->
                    <img src="/content/images/report/YChart.png" />
                </div>
                <div class="form-group">
                    <input id="PiotLineChart" name="PilotChartType" type="radio" value="Line" class="inline" />
                    <label for="PiotLineChart">@app.T["line_chart"]</label><!--折线图：-->
                    <img src="/content/images/report/lineChart.png" />
                </div>
                <div class="form-group">
                    <input id="PiotCakeChart" name="PilotChartType" type="radio" value="Pie" class="inline" />
                    <label for="PiotCakeChart">@app.T["chart_pie"]</label><!--饼图：-->
                    <img src="/content/images/report/cakeChart.png" />
                </div>
            </div>
            <div id="pliot-page5" class="modal-body" data-page="5" style="display:none;">
                <div class="form-group OtherChart" style="line-height:35px;">
                    <label>@app.T["set_Xaxis_format"]</label><!--设置（X）轴的格式-->
                    <div class="row">
                        <div class="col-sm-2">
                            <label for="Chart-Field">@app.T["report_field"]</label>                            <!--字段*:-->
                        </div>
                        <div class="col-sm-10">
                            <select id="Chart-Field" name="Chart-Field" class="form-control"></select>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-2">
                            <label for="Chart-Label">@app.T["report_table"]</label><!--标签*:-->
                        </div>
                        <div class="col-sm-10">
                            <input id="Chart-Label" name="Chart-Label" type="text" value="" class="form-control" />
                        </div>
                    </div>
                </div>
                <div class="form-group OtherChart" style="line-height:35px;">
                    <label>@app.T["set_Yaxis_format"]</label><!--设置（Y）轴的格式-->
                    <div class="row">
                        <div class="col-sm-3">
                            <label for="Chart-FirstField">@app.T["first_data"]</label><!--第一个值的数据*:-->
                        </div>
                        <div class="col-sm-9">
                            <select id="Chart-FirstField" name="Chart-FirstField" class="form-control"></select>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-3">
                            <label for="Chart-FirstLabel">@app.T["first_table"]</label><!--第一个值的标签*:-->
                        </div>
                        <div class="col-sm-9">
                            <input id="Chart-FirstLabel" name="Chart-FirstLabel" type="text" value="" class="form-control" />
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-3">
                            <label for="Chart-SecField">@app.T["second_data"]</label>
                        </div>
                        <div class="col-sm-9">
                            <select id="Chart-SecField" name="Chart-SecField" class="form-control"></select>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-3">
                            <label for="Chart-SecLabel">@app.T["second_table"]</label>
                        </div>
                        <div class="col-sm-9">
                            <input id="Chart-SecLabel" name="Chart-SecLabel" type="text" value="" class="form-control" />
                        </div>
                    </div>
                </div>
                <div class="form-group CakeChart" style="line-height:35px;">
                    <label>@app.T["report_slice_up"]</label>
                    <div class="row">
                        <div class="col-sm-2">
                            <label for="Chart-Section">@app.T["report_data_colon"]</label>
                        </div>
                        <div class="col-sm-10">
                            <select id="Chart-Section" name="Chart-Section" class="form-control"></select>
                        </div>
                    </div>
                </div>
                <div class="form-group CakeChart" style="line-height:35px;">
                    <label>@app.T["report_value"]</label>
                    <div class="row">
                        <div class="col-sm-2">
                            <label for="Chart-Value">@app.T["report_data_colon"]</label>
                        </div>
                        <div class="col-sm-10">
                            <select id="Chart-Value" name="Chart-Value" class="form-control"></select>
                        </div>
                    </div>
                </div>
                <div class="form-group" style="line-height:35px;">
                    <label>@app.T["set_table_cutline_format"]</label>
                    <div class="row">
                        <div class="col-sm-3">
                            <label for="Chart-isShowLabel">@app.T["show_datatable"]</label>
                            <input type="checkbox" id="Chart-isShowLabel" name="Chart-isShowLabel" value="Chart-isShowLabel" checked="checked" />
                        </div>
                        <div class="col-sm-8">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-3">
                            <label for="Chart-isShowText">@app.T["show_cutline"]</label>
                            <input type="checkbox" id="Chart-isShowText" name="Chart-isShowText" value="Chart-isShowText" checked="checked" />
                        </div>
                        <div class="col-sm-8">
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default"
                        onclick="CancelPilot()">
                    <span class="glyphicon glyphicon-remove"></span> <span class="NextText">@app.T["cancel"]</span>
                </button>
                <button type="button" class="btn btn-primary" onclick="PrevPage()">
                    <span class="glyphicon glyphicon-circle-arrow-left"></span> <span class="NextText">@app.T["back"]</span>
                </button>
                <button type="button" id="NextBtn" class="btn btn-primary" onclick="savePilot()">
                    <span class="glyphicon glyphicon-circle-arrow-right"></span> <span class="NextText">@app.T["next_step"]</span>
                </button>
            </div>
        </div>
        <!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>
<div class="modal fade" id="AddFieldModal" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-hidden="true">
                    @app.T["times_sign"]
                </button>
                <h4 class="modal-title" id="AddFieldModalLabel">
                    @app.T["report_wizard"]
                </h4>
            </div>
            <div class="modal-body" style="line-height:29px;text-align:right;">
                <div class="form-group">
                    <div class="row">
                        <div class="col-sm-2">
                            <label for="AddField-recordtype">@app.T["record_type"]</label>
                        </div>
                        <div class="col-sm-8">
                            <select id="AddField-recordtype" name="AddField-recordtype" class="form-control"></select>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="row">
                        <div class="col-sm-2">
                            <label for="AddField-column">@app.T["report_column"]</label>
                        </div>
                        <div class="col-sm-8">
                            <select id="AddField-column" name="AddField-column" class="form-control"></select>
                        </div>
                    </div>
                </div>
                <div id="AddField-timeBox" style="display:none;" class="form-group isGroup">
                    <div class="row">
                        <div class="col-sm-2">
                            <label for="AddField-time">@app.T["time_interval"]</label>
                        </div>
                        <div class="col-sm-8">
                            <select id="AddField-time" name="AddField-time" class="form-control">
                                <option value="Day">@app.T["day"]</option>
                                <option value="Weed">@app.T["week"]</option>
                                <option value="Month">@app.T["month"]</option>
                                <option value="Year">@app.T["year"]</option>
                                <option value="AccountingPeriod">@app.T["accounting_period"]</option>
                                <option value="FiscalYear">@app.T["accounting_year"]</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="row">
                        <div class="col-sm-2">
                            <label for="AddField-type">@app.T["data_type"]</label>
                        </div>
                        <div style="text-align:left;" class=" col-sm-8">
                            <span id="AddField-type">
                            </span>
                        </div>
                    </div>
                </div>
                <div class="form-group isGroup">
                    <div class="row">
                        <div class="col-sm-2">
                            <label for="AddField-sort">@app.T["sort_order"]</label>
                        </div>
                        <div class="col-sm-8">
                            <select id="AddField-sort" name="AddField-sort" class="form-control">
                                <option value="Ascending">@app.T["asc_order"]</option>
                                <option value="Descending">@app.T["desc_order"]</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="row">
                        <div class="col-sm-2">
                            <label for="AddField-width">@app.T["col_width"]</label>
                        </div>
                        <div class="col-sm-8">
                            <select id="AddField-width" name="AddField-width" class="form-control">
                                <option value="25">25@(app.T["pixel"])</option>
                                <option value="50">50@(app.T["pixel"])</option>
                                <option value="75">75@(app.T["pixel"])</option>
                                <option value="100">100@(app.T["pixel"])</option>
                                <option value="125">125@(app.T["pixel"])</option>
                                <option value="150">150@(app.T["pixel"])</option>
                                <option value="200">200@(app.T["pixel"])</option>
                                <option value="300">300@(app.T["pixel"])</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="row">
                        <div class="col-sm-2">
                            <label for="AddField-abstract">@app.T["abstract_type"]</label>
                        </div>
                        <div class="col-sm-8">
                            <select id="AddField-abstract" name="AddField-abstract" class="form-control">
                                <option data-value="" value="">@app.T["nothing"]</option>
                                <option data-type="other" data-value="count" value="4">@app.T["count"]</option>
                                <option data-type="int" data-value="sum" value="0">@app.T["sum_total"]</option>
                                <option data-type="int" data-value="max" value="1">@app.T["maximum_value"]</option>
                                <option data-type="int" data-value="min" value="2">@app.T["minimum_value"]</option>
                                <option data-type="int" data-value="avg" value="3">@app.T["average_value"]</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="form-group isColumn" style="text-align:left;">
                    <input id="AddField-format" name="AddField-format" type="checkbox" />
                    <label for="AddField-format">@app.T["contain_notform_valueline"]</label><!--包含无格式值列。-->
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default"
                        data-dismiss="modal">
                    <span class="glyphicon glyphicon-remove"></span> @app.T["cancel"]
                </button>
                <button type="button" class="btn btn-primary goon-btn" onclick="insertField(true)">
                    <span class="glyphicon glyphicon-ok"></span> 继续添加
                </button>
                <button type="button" class="btn btn-primary" onclick="insertField()">
                    <span class="glyphicon glyphicon-ok"></span> @app.T["notarize"]
                </button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="PilotSortModal" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-hidden="true">
                    @app.T["times_sign"]
                </button>
                <h4 class="modal-title" id="PilotSortModalLabel">
                    @app.T["configure_sort_order"]
                </h4><!--配置排序顺序-->
            </div>
            <div class="modal-body">
                <div class="form-group">
                    <label>@app.T["select_defaults_sortline"]</label>
                </div><!--选择在默认情况下排序时所依据的列-->

                <div class="form-group">
                    <div class="row">
                        <div class="col-sm-2">
                            <label for="PilotSort-Sort">@app.T["sort_order"]</label>
                        </div>
                        <div class="col-sm-8">
                            <select id="PilotSort-Sort" name="PilotSort-Sort" class="form-control">
                                <option data-text="1" value="1">1</option>
                                <option data-text="11" value="11">11</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="row">
                        <div class="col-sm-2">
                            <label for="PilotSort-SortType"></label>
                        </div>
                        <div class="col-sm-8">
                            <input name="PilotSort-SortType" type="radio" value="Ascending" checked="checked" />@app.T["asc_order"]
                            <input name="PilotSort-SortType" type="radio" value="Descending" />@app.T["desc_order"]
                        </div>
                    </div>
                </div>
            </div>
            <!-- /.modal-content -->
            <div class="modal-footer">
                <button type="button" class="btn btn-default"
                        data-dismiss="modal">
                    <span class="glyphicon glyphicon-remove"></span> @app.T["cancel"]
                </button>
                <button type="button" class="btn btn-primary" onclick="HidePilotSortModal()">
                    <span class="glyphicon glyphicon-ok"></span> @app.T["confirm"]
                </button>
            </div>
        </div><!-- /.modal-dialog -->
    </div>
</div>
<div class="modal fade" id="SetRowModal" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-hidden="true">
                    @app.T["times_sign"]
                </button>
                <h4 class="modal-title" id="SetRowModalLabel">
                    @app.T["set_show_arry"]
                </h4>
            </div>
            <div class="modal-body">
                <div class="form-group">
                    <label>@app.T["select_prompt"]</label>
                </div>
                <div class="form-group">
                    <div class="row">
                        <div class="col-sm-2">
                            <label for="SetRow-Field">@app.T["aggregate"]</label><!--汇总值:-->
                        </div>
                        <div class="col-sm-8">
                            <select id="SetRow-Field" name="SetRow-Field" class="form-control"></select>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="row">
                        <div class="col-sm-2">
                            <label for="SetRow-direction">@app.T["top_few_number"]</label><!--最前/最高几个:-->
                        </div>
                        <div class="col-sm-8">
                            <select id="SetRow-direction" name="SetRow-direction" class="form-control">
                                <option value="Not">@app.T["unlimited"]</option>
                                <option value="TopN">@app.T["top"]</option>
                                <option value="BottomN">@app.T["bottom"]</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="row">
                        <div class="col-sm-2">
                            <label for="SetRow-rownum">@app.T["class_number"]</label>
                        </div>
                        <div class="col-sm-8">
                            <input id="SetRow-rownum" value="0" name="SetRow-rownum" class="form-control" />
                        </div>
                    </div>
                </div>
            </div>
            <!-- /.modal-content -->
            <div class="modal-footer">
                <button type="button" class="btn btn-default" onclick="HideSetRowModal()"
                        data-dismiss="modal">
                    <span class="glyphicon glyphicon-remove"></span> @app.T["cancel"]
                </button>
                <button type="button" class="btn btn-primary" onclick="SaveSetRow()">
                    <span class="glyphicon glyphicon-ok"></span> @app.T["confirm"]
                </button>
            </div>
        </div><!-- /.modal-dialog -->
    </div>
</div>
<script src="@app.PlatformSettings.ScriptCDN/content/js/filterdialog.js?v=@app.PlatformSettings.VersionNumber"></script>
<script src="@app.PlatformSettings.ScriptCDN/content/js/bootstrap-datepicker-1.5.0/js/bootstrap-datepicker.min.js?v=@app.PlatformSettings.VersionNumber"></script>
<script src="@app.PlatformSettings.ScriptCDN/content/js/bootstrap-datepicker-1.5.0/locales/bootstrap-datepicker.zh-CN.min.js?v=@app.PlatformSettings.VersionNumber" charset="UTF-8"></script>
<script src="@app.PlatformSettings.ScriptCDN/content/js/fetch.js?v=@app.PlatformSettings.VersionNumber"></script>
@{await Html.RenderPartialAsync("FetchLabel"); }
<script>
    console.log(1);

    var EntityRelationship = $('#EntityList').find('option:selected').attr('data-relationship');
    var EntityName = EntityRelationship;
    var EntityId = $('#EntityList').find('option:selected').val();
    var EntityText = $('#EntityList').find('option:selected').text();
    var RelevantEntityId = $('#RelevantEntityList').find('option:selected').val();
    var RelevantEntityName = '';
    var RelevantText = '';
    var RelevantReferencingattributelocalizedname = '';
    var RelevantReferencedentityname = '';
    var RelevantReferencingattributename = '';
    var RelevantReferencedattributename = '';
    var RelevantReferencingentityname = '';
    var RelevantReferencingentitylocalizedname = '';
    if (RelevantEntityId != '') {
        RelevantEntityName = $('#RelevantEntityList').find('option:selected').attr('data-relationship');
        RelevantText = $('#RelevantEntityList').find('option:selected').text();
        RelevantReferencingattributelocalizedname = $('#RelevantEntityList').find('option:selected').attr('data-referencingattributelocalizedname');
        RelevantReferencedentityname = $('#RelevantEntityList').find('option:selected').attr('data-referencedentityname');
        RelevantReferencingattributename = $('#RelevantEntityList').find('option:selected').attr('data-referencingattributename');
        RelevantReferencedattributename = $('#RelevantEntityList').find('option:selected').attr('data-referencedattributename');
        RelevantReferencingentityname = $('#RelevantEntityList').find('option:selected').attr('data-referencingentityname');
        RelevantReferencingentitylocalizedname = $('#RelevantEntityList').find('option:selected').attr('data-referencingentitylocalizedname');
    }
    var Relationships;
    var ReportEntity = {
        ParamName: '',
        Query: new Array()
    };
    var ReportPage2Entity = {
        ParamName: '',
        Query: new Array()
    };
    var CustomReport = {
        Groupings: new Array(),
        Columns: new Array(),
        Filter: null,
        Query: new Array(),
        TableLayout: '',
        Chart: null
    };
    var Chart = { Type: '', Legend: true, DataLabels: true, ValueAxes: new Array(), ColumnAxis: { Field: '', Name: '' } };
    var inttype = ['money', 'int', 'float'];
    $(function () {
        $('#PilotModal').modal('show');
        $('#PilotModal').modal({ backdrop: 'static', keyboard: false });

        //$('#NextBtn').bind('click', function () {
        //    NextPage();
        //});
        Xms.Web.OpenDialog('/filter/screenconditions', function (data) {
            $('#PilotModal .modal-content').find('div:eq(0)').after(data);
            //page1编辑状态
            if (isEditReport) {
                var editDefaultFilterConfig = $('#edit-DefaultFilterConfig').val();
                editDefaultFilterConfig = JSON.parse(editDefaultFilterConfig);
                var entityFilter = editDefaultFilterConfig[0];
                var relatedEntityFilter = editDefaultFilterConfig[1];
                initFilter(entityFilter, $('#pliot-page1').find('div[data-type="Main"]'));
                initFilter(relatedEntityFilter, $('#pliot-page1').find('div[data-type="Relevant"]'));

            }
        });
        //page2编辑状态
        if (isEditReport) {
            EditPage2();
            EditPage3();
            EditPage4();
            EditPage5();
        }

        //第二页绑定时间
        $('body').off('click', '#GroupList .group-null:first');
        $('body').on('click', '#GroupList .group-null:first', function () {
            openAddField('group');
        });
        $('body').off('change', '#AddField-recordtype');
        $('body').on('change', '#AddField-recordtype', function () {
            var type = $('#AddFieldModal').attr('data-type');
            loadAttributes(type);
        });
        $('body').off('change', '#AddField-column');
        $('body').on('change', '#AddField-column', function () {
            $('#AddField-type').text($(this).find('option:selected').attr('data-type'));
            if ($(this).find('option:selected').attr('data-type') == 'datetime' && $('#AddFieldModal').attr('data-type') == 'group') {
                $('#AddField-time').removeProp('disabled');
                $('#AddField-timeBox').show();
            } else {
                $('#AddField-time').prop('disabled', 'disabled');
                $('#AddField-timeBox').hide();
            }
            SummaryValue($(this).find('option:selected').attr('data-type'));
        });
        //第三页选项事件
        $('body').off('change', 'input[name="PilotFormat"]');
        $('body').on('change', 'input[name="PilotFormat"]', function () {
            var choose = $('#pliot-page3').find('input[name="PilotFormat"]:checked');
            if (choose.prop('id') == 'OnlyTable') {
                $('#NextBtn').find('.NextText').text('完成');
                //$('#NextBtn').unbind();
                //$('#NextBtn').bind('click', function () {
                //    savePilot();
                //});
            }
            else {
                $('#NextBtn').find('.NextText').text('下一步');
                //$('#NextBtn').unbind();
                //$('#NextBtn').bind('click', function () {
                //    NextPage();
                //    console.log(111);
                //});
            }
        });
        //第五页选项事件
        $('body').off('change', '#Chart-Field');
        $('body').on('change', '#Chart-Field', function () {
            var choose = $(this).find('option:selected');
            $(this).attr('data-relationship', choose.attr('data-relationship'));
            $(this).attr('data-name', choose.val());
            $(this).attr('data-entityid', choose.attr('data-entityid'));
            $('#Chart-Label').val($(this).find('option:selected').text());
        });
        $('body').off('change', '#Chart-FirstField');
        $('body').on('change', '#Chart-FirstField', function () {
            var choose = $(this).find('option:selected');
            $(this).attr('data-relationship', choose.attr('data-relationship'));
            $(this).attr('data-name', choose.val());
            $(this).attr('data-entityid', choose.attr('data-entityid'));
            $('#Chart-FirstLabel').val($(this).find('option:selected').text());
            $('#Chart-SecField').find('option').show();
            $('#Chart-SecField').find('option[data-entityid="' + choose.attr('data-entityid') + '"][value="' + choose.val() + '"]').hide();
        });
        $('body').off('change', '#Chart-SecField');
        $('body').on('change', '#Chart-SecField', function () {
            var choose = $(this).find('option:selected');
            if (choose.val() != '') {
                $(this).attr('data-relationship', choose.attr('data-relationship'));
                $(this).attr('data-name', choose.val());
                $(this).attr('data-entityid', choose.attr('data-entityid'));
                $('#Chart-SecLabel').val($(this).find('option:selected').text());
            } else {
                $('#Chart-SecLabel').val('');
            }
            $('#Chart-FirstField').find('option').show();
            $('#Chart-FirstField').find('option[data-entityid="' + choose.attr('data-entityid') + '"][value="' + choose.val() + '"]').hide();
        });
        $('body').off('change', '#Chart-Section');
        $('body').on('change', '#Chart-Section', function () {
            var choose = $(this).find('option:selected');
            $(this).attr('data-relationship', choose.attr('data-relationship'));
            $(this).attr('data-name', choose.val());
            $(this).attr('data-entityid', choose.attr('data-entityid'));
        });
        $('body').off('change', '#Chart-Value');
        $('body').on('change', '#Chart-Value', function () {
            var choose = $(this).find('option:selected');
            if (choose.val() != '') {
                $(this).attr('data-relationship', choose.attr('data-relationship'));
                $(this).attr('data-name', choose.val());
                $(this).attr('data-entityid', choose.attr('data-entityid'));
            }
        });
        //第五页触发保存
        $('body').on('change', '#pliot-page5 input,#pliot-page5 select', function () {
            SavePage5();
        });

    });

    function EditPage2() {
        var editCustomConfig = JSON.parse($('#edit-CustomConfig').val());
        var editColumns = editCustomConfig.Columns;
        var editGroupings = editCustomConfig.Groupings;
        Xms.Schema.GetEntityRelations(EntityId, null, function (data) {
            if (!data || data.Content.length == 0) return;
            var Entityitem = '<option  data-relentity="false"  data-relationship="" data-referencingattributename="" data-referencedattributename="" data-referencingattributelocalizedname="" value="' + EntityId + '">' + EntityText + '</option>';
            if (RelevantEntityName != '') {
                var relEntityitem = '<option data-referencingentitylocalizedname="' + RelevantReferencingentitylocalizedname + '" data-relentity="true"  data-relationship="' + RelevantEntityName + '" data-referencingattributename="' + RelevantReferencingattributename + '" data-referencedattributename="' + RelevantReferencedattributename + '" value="' + RelevantEntityId + '">' + RelevantText + '</option>';
            }
            $('#AddField-recordtype').append(Entityitem);
            $('#AddField-recordtype').append(relEntityitem);
            $('#AddField-recordtype').append('<optgroup label="与 ' + EntityText + ' 相关">');
            $(JSON.parse(data.Content)).each(function (i, n) {
                $('#AddField-recordtype').append('<option data-relentity="false" data-relationship="' + n.name + '" data-referencingattributelocalizedname="' + n.referencingattributelocalizedname + '" data-referencingattributename="' + n.referencingattributename + '" data-referencedattributename="' + n.referencedattributename + '" value="' + n.referencedentityid + '">' + n.referencingattributelocalizedname + '(' + n.referencedentitylocalizedname + ')' + '</option>');
            });

        });
        Xms.Schema.GetEntityRelations(RelevantEntityId, null, function (reldata) {
            if (!reldata || reldata.Content.length == 0) return;
            $('#AddField-recordtype').append('<optgroup label="与 ' + RelevantText + ' 相关">');
            $(JSON.parse(reldata.Content)).each(function (i, n) {
                if (EntityId.toLowerCase() != n.referencedentityid.toLowerCase()) {
                    $('#AddField-recordtype').append('<option data-relentity="true" data-relationship="' + n.name + '" data-referencingattributelocalizedname="' + n.referencingattributelocalizedname + '" data-referencingattributename="' + n.referencingattributename + '" data-referencedattributename="' + n.referencedattributename + '"  value="' + n.referencedentityid + '">' + n.referencingattributelocalizedname + '(' + n.referencedentitylocalizedname + ')' + '</option>');
                }
            });
            $('#AddField-recordtype').append('</optgroup>');
        });
        for (var i = 0; i < editGroupings.length; i++) {
            $('#AddFieldModal').attr('data-type', 'group');
            $('#AddFieldModal').attr('data-isnew', 'true');
            var editAlias = "";
            var editName = "";
            var editRelationship = "";
            var editEntityName = "";
            if (editGroupings[i].Field.indexOf('.') > -1) {
                editRelationship = editGroupings[i].Field.split('.')[0];
                editAlias = editGroupings[i].Field.split('.')[0];
                editName = editGroupings[i].Field.split('.')[1];
                editEntityName = editRelationship.split('_')[2];
            } else {
                editName = editGroupings[i].Field;
            }
            var editAbstract = editGroupings[i].SummaryValue;//摘要类型
            var editWidth = editGroupings[i].Width;//列宽
            var editSort = editGroupings[i].Sort;
            //选择实体
            $('#AddField-recordtype').find('option').each(function (i, n) {
                if (editRelationship.toLowerCase() == $(n).attr('data-relationship').toLowerCase()) {
                    $(n).attr('selected', true);
                }
            });
            //选择字段
            var selentityid = $('#AddField-recordtype').find('option:selected').val() || '';
            var selrelationship = $('#AddField-recordtype').find('option:selected').attr('data-relationship') || '';
            var selreferencingattributelocalizedname = $('#AddField-recordtype').find('option:selected').attr('data-referencingattributelocalizedname') || '';
            var isrelentity = $('#AddField-recordtype').find('option:selected').attr('data-relentity');
            Xms.Web.AsyncGet('/api/schema/attribute/getbyentityid/' + selentityid + '/' + editName, function (data) {
                data = JSON.parse(data.Content);
                $('#AddField-column').html('<option  data-isrelentity="' + isrelentity + '" data-text="' + data.localizedname + '" data-relationship="' + selrelationship + '" data-referencingattributelocalizedname="' + selreferencingattributelocalizedname + '" data-type="' + data.attributetypename + '"  value="' + data.name + '" data-entityname="' + data.entityname + '" data-entitylocalizedname="' + data.entitylocalizedname + '" selected="selected">' + data.localizedname + '</option>');
                $('#AddField-type').text($('#AddField-column').find('option:selected').attr('data-type'));
                if ($('#AddField-column').find('option:selected').attr('data-type') == 'datetime') {
                    $('#AddField-time').removeProp('disabled');
                    $('#AddField-timeBox').show();
                } else {
                    $('#AddField-time').prop('disabled', 'disabled');
                    $('#AddField-timeBox').hide();
                }
                SummaryValue($('#AddField-column').find('option:selected').attr('data-type'));
            });
            $('#AddField-abstract').find('option[data-value="' + editAbstract + '"]').attr('selected', true);
            $('#AddField-sort').find('option[value="' + editSort + '"]').attr('selected', true);
            $('#AddField-width').find('option[value="' + editWidth + '"]').attr('selected', true);
            insertField();
        }
        for (var i = 0; i < editColumns.length; i++) {
            $('#AddFieldModal').attr('data-type', 'column');
            $('#AddFieldModal').attr('data-isnew', 'true');
            var editAlias = "";
            var editName = "";
            var editRelationship = "";
            var editEntityName = "";
            if (editColumns[i].Field.indexOf('.') > -1) {
                editRelationship = editColumns[i].Field.split('.')[0];
                editAlias = editColumns[i].Field.split('.')[0];
                editName = editColumns[i].Field.split('.')[1];
                editEntityName = editRelationship.split('_')[2];
            } else {
                editName = editColumns[i].Field;
            }
            var editAbstract = editColumns[i].SummaryValue;//摘要类型
            var editWidth = editColumns[i].Width;//列宽
            var editSort = editColumns[i].Sort;
            //选择实体
            $('#AddField-recordtype').find('option').each(function (i, n) {
                if (editRelationship.toLowerCase() == $(n).attr('data-relationship').toLowerCase()) {
                    $(n).attr('selected', true);
                }
            });
            //选择字段
            var selentityid = $('#AddField-recordtype').find('option:selected').val() || '';
            var selrelationship = $('#AddField-recordtype').find('option:selected').attr('data-relationship') || '';
            var selreferencingattributelocalizedname = $('#AddField-recordtype').find('option:selected').attr('data-referencingattributelocalizedname') || '';
            var isrelentity = $('#AddField-recordtype').find('option:selected').attr('data-relentity');
            Xms.Web.AsyncGet('/api/schema/attribute/getbyentityid/' + selentityid + '/' + editName, function (data) {
                data = JSON.parse(data.Content);
                $('#AddField-column').html('<option  data-isrelentity="' + isrelentity + '" data-text="' + data.localizedname + '" data-relationship="' + selrelationship + '" data-referencingattributelocalizedname="' + selreferencingattributelocalizedname + '" data-type="' + data.attributetypename + '"  value="' + data.name + '" data-entityname="' + data.entityname + '" data-entitylocalizedname="' + data.entitylocalizedname + '" selected="selected">' + data.localizedname + '</option>');
                $('#AddField-type').text($('#AddField-column').find('option:selected').attr('data-type'));
                if ($('#AddField-column').find('option:selected').attr('data-type') == 'datetime') {
                    $('#AddField-time').removeProp('disabled');
                    $('#AddField-timeBox').show();
                } else {
                    $('#AddField-time').prop('disabled', 'disabled');
                    $('#AddField-timeBox').hide();
                }
                SummaryValue($('#AddField-column').find('option:selected').attr('data-type'));
            });
            $('#AddField-abstract').find('option[data-value="' + editAbstract + '"]').attr('selected', true);
            $('#AddField-width').find('option[value="' + editWidth + '"]').attr('selected', true);
            insertField();
            if (editSort != null && editSort != '') {
                $('#PilotSort-Sort').find('option[value="' + editColumns[i].Field + '"]').attr('selected', true);
                $('#PilotSortModal').find('input[name="PilotSort-SortType"][value="' + editSort + '"]').attr('checked', true);
            }
        }
        //设置要显示的组数
        var editFilter = editCustomConfig.Filter;
        if (editFilter != null && editFilter != '') {
            $('#SetRow-Field').find('option[value="' + editFilter.Filed + '"]').attr('selected', true);
            $('#SetRow-direction').find('option[value="' + editFilter.Operator + '"]').attr('selected', true);
            $('#SetRow-rownum').val(editFilter.Value);
        }

        isEditReport = true;
    }

    function EditPage3() {
        var editCustomConfig = JSON.parse($('#edit-CustomConfig').val());
        var tableLayout = editCustomConfig.TableLayout;
        $('#pliot-page3').find('input[name="PilotFormat"][value="' + tableLayout + '"]').attr('checked', true);
    }

    function EditPage4() {
        var editCustomConfig = JSON.parse($('#edit-CustomConfig').val());
        if (editCustomConfig.Chart != null && editCustomConfig.Chart != '') {
            var chartType = editCustomConfig.Chart.Type;
            $('#pliot-page4').find('input[name="PilotChartType"][value="' + chartType + '"]').attr('checked', true);
        }
    }

    function EditPage5() {
        var editCustomConfig = JSON.parse($('#edit-CustomConfig').val());
        if (editCustomConfig.Chart != null && editCustomConfig.Chart != '') {
            if ($('#pliot-page4').find('input[name="PilotChartType"]:checked').val() == 'Pie') {
                var axis = editCustomConfig.Chart.ColumnAxis.Field;
                var axisList = axis.split('_');
                var firVal = editCustomConfig.Chart.ValueAxes[0].Field;
                var firName = editCustomConfig.Chart.ValueAxes[0].Name;
                var firValList = firVal.split('_');
                var axisEntity = EntityName.toLowerCase();
                var axisVal = axis;
                $('#Chart-Section').find('option').each(function (i, n) {
                    if (axisList.length > 1) {
                        axisEntity = axisList[0].toLowerCase();
                        axisVal = axisList[1].toLowerCase();
                    }
                    if ($(n).attr('data-entityname').toLowerCase() == axisEntity && $(n).val().toLowerCase() == axisVal) {
                        $(n).attr('selected', true);
                    }
                });

                var firEntity = EntityName.toLowerCase();
                var firVal = firVal.toLowerCase();
                $('#Chart-Value').find('option').each(function (i, n) {
                    if (firValList.length > 1) {
                        firEntity = firValList[0].toLowerCase();
                        firVal = firValList[1].toLowerCase();
                    }
                    if ($(n).attr('data-entityname').toLowerCase() == firEntity && $(n).val().toLowerCase() == firVal) {
                        $(n).attr('selected', true);
                    }
                });

            } else {
                var axis = editCustomConfig.Chart.ColumnAxis.Field;
                var axisList = axis.split('_');
                var axisName = editCustomConfig.Chart.ColumnAxis.Name;
                var firVal = editCustomConfig.Chart.ValueAxes[0].Field;
                var firName = editCustomConfig.Chart.ValueAxes[0].Name;
                var firValList = firVal.split('_');
                var axisEntity = EntityName.toLowerCase();
                var axisVal = axis.toLowerCase();
                $('#Chart-Field').find('option').each(function (i, n) {
                    if (axisList.length > 1) {
                        axisEntity = axisList[0].toLowerCase();
                        axisVal = axisList[1].toLowerCase();
                    }
                    if ($(n).attr('data-entityname').toLowerCase() == axisEntity && $(n).val().toLowerCase() == axisVal) {
                        $(n).attr('selected', true);
                    }
                });
                $('#Chart-Label').val(axisName);

                var firEntity = EntityName.toLowerCase();
                var firVal = firVal.toLowerCase();
                $('#Chart-FirstField').find('option').each(function (i, n) {
                    if (firValList.length > 1) {
                        firEntity = firValList[0].toLowerCase();
                        firVal = firValList[1].toLowerCase();
                    }
                    if ($(n).attr('data-entityname').toLowerCase() == firEntity && $(n).val().toLowerCase() == firVal) {
                        $(n).attr('selected', true);
                    }
                });
                $('#Chart-FirstLabel').val(firName);

                if (editCustomConfig.Chart.ValueAxes.length > 1) {
                    var secVal = editCustomConfig.Chart.ValueAxes[1].Field;
                    var secName = editCustomConfig.Chart.ValueAxes[1].Name;
                    var secValList = secVal.split('_');
                    var secEntity = EntityName.toLowerCase();
                    var secVal = secVal.toLowerCase();
                    $('#Chart-SecField').find('option').each(function (i, n) {
                        if (secValList.length > 1) {
                            firEntity = secValList[0].toLowerCase();
                            secVal = secValList[1].toLowerCase();
                        }
                        if ($(n).attr('data-entityname').toLowerCase() == secEntity && $(n).val().toLowerCase() == secVal) {
                            $(n).attr('selected', true);
                        }
                    });
                    $('#Chart-SecLabel').val(secName);
                }
            }

            var isshowLable = editCustomConfig.Chart.DataLabels;
            var legend = editCustomConfig.Chart.Legend;
            if (isshowLable == true) {
                $('#Chart-isShowLabel').attr('checked', true);
            } else {
                $('#Chart-isShowLabel').attr('checked', false);
            }
            if (legend == true) {
                $('#Chart-isShowText').attr('checked', true);
            } else {
                $('#Chart-isShowText').attr('checked', false);
            }
        }

    }

    function SummaryValue(type) {
        if ($.inArray(type, inttype) != -1) {
            $('#AddField-abstract').find('option:eq(0)').prop('selected', true);
            $('#AddField-abstract').find('option[data-type="int"]').show();
        }
        else {
            $('#AddField-abstract').find('option:eq(0)').prop('selected', true);
            $('#AddField-abstract').find('option[data-type="int"]').hide();
        }
    }
    //加载字段
    function loadAttributes(type) {
        var entityid = $('#AddField-recordtype').find('option:selected').val() || '';
        var relationship = $('#AddField-recordtype').find('option:selected').attr('data-relationship') || '';
        var referencingattributelocalizedname = $('#AddField-recordtype').find('option:selected').attr('data-referencingattributelocalizedname') || '';
        var isrelentity = $('#AddField-recordtype').find('option:selected').attr('data-relentity');
        var _html = new Array();
        Xms.Schema.GetAttributes({ getall: true, entityid: entityid }, function (data) {
            if (!data || data.length == 0) return;
            $(data).each(function (i, n) {
                if (attributeIsExists(n.entityname, n.name, type, relationship) == true) {
                    _html.push('<option data-isrelentity="' + isrelentity + '" data-text="' + n.localizedname + '" data-relationship="' + relationship + '" data-referencingattributelocalizedname="' + referencingattributelocalizedname + '" data-type="' + n.attributetypename + '"  value="' + n.name + '" data-entityname="' + n.entityname + '" data-entitylocalizedname="' + n.entitylocalizedname + '">');
                    _html.push(n.localizedname);
                    _html.push('</option>');
                }
            });
            $('#AddField-column').html(_html.join(''));
            $('#AddField-type').text($('#AddField-column').find('option:selected').attr('data-type'));
            if ($('#AddField-column').find('option:selected').attr('data-type') == 'datetime') {
                $('#AddField-time').removeProp('disabled');
                $('#AddField-timeBox').show();
            } else {
                $('#AddField-time').prop('disabled', 'disabled');
                $('#AddField-timeBox').hide();
            }
            SummaryValue($('#AddField-column').find('option:selected').attr('data-type'));
        });
    }
    //判断字段是否已加入列表
    function attributeIsExists(entityName, attrName, type, relationship) {
        entityName = entityName.toLowerCase();
        attrName = attrName.toLowerCase();
        //是否为关联实体字段
        //是否已存在
        var exist = true;
        if (type == 'group') {
            $('.group-row').each(function (ii, nn) {
                if ($(nn).find('.group-null').length <= 0) {
                    if ($(nn).attr('data-cname').toLowerCase() == attrName && $(nn).attr('data-centityname').toLowerCase() == entityName && $(nn).attr('data-crelationship').toLowerCase() == relationship.toLowerCase()) {
                        exist = false;
                        return false;
                    }
                }
            });
        } else {
            $('#ColumnField').find('th').not('.AddBtn').each(function (ii, nn) {
                if ($(nn).attr('data-cname').toLowerCase() == attrName && $(nn).attr('data-centityname').toLowerCase() == entityName && $(nn).attr('data-crelationship').toLowerCase() == relationship.toLowerCase()) {
                    exist = false;
                    return false;
                }
            });
        }
        if (exist == false) {
            return false;
        }
        else {
            return true;
        }
        return false;
    }
    //加载实体，两个字段同时引用同一实体时？
    function loadEntities() {
        var entityid = EntityId;
        var entityname = EntityRelationship;
        Xms.Schema.GetEntityRelations(entityid, null, function (data) {
            if (!data || data.content.length == 0) return;
            var Entityitem = '<option  data-relentity="false"  data-relationship="" data-referencingattributename="" data-referencedattributename="" data-referencingattributelocalizedname="" value="' + entityid + '">' + EntityText + '</option>';
            if (RelevantEntityName != '') {
                var relEntityitem = '<option data-referencingentitylocalizedname="' + RelevantReferencingentitylocalizedname + '" data-relentity="true"  data-relationship="' + RelevantEntityName + '" data-referencingattributename="' + RelevantReferencingattributename + '" data-referencedattributename="' + RelevantReferencedattributename + '" value="' + RelevantEntityId + '">' + RelevantText + '</option>';
            }
            $('#AddField-recordtype').append(Entityitem);
            $('#AddField-recordtype').append(relEntityitem);
            $('#AddField-recordtype').append('<optgroup label="与 ' + EntityText + ' 相关">');
            $(data.content).each(function (i, n) {
                $('#AddField-recordtype').append('<option data-relentity="false" data-relationship="' + n.name + '" data-referencingattributelocalizedname="' + n.referencingattributelocalizedname + '" data-referencingattributename="' + n.referencingattributename + '" data-referencedattributename="' + n.referencedattributename + '" value="' + n.referencedentityid + '">' + n.referencingattributelocalizedname + '(' + n.referencedentitylocalizedname + ')' + '</option>');
            });
            $('#AddField-recordtype').append('</optgroup>');
            if (RelevantEntityName != '') {
                Xms.Schema.GetEntityRelations(RelevantEntityId, null, function (reldata) {
                    if (!data || data.content.length == 0) return;
                    $('#AddField-recordtype').append('<optgroup label="与 ' + RelevantText + ' 相关">');
                    $(reldata.content).each(function (i, n) {
                        if (EntityId.toLowerCase() != n.referencedentityid.toLowerCase()) {
                            $('#AddField-recordtype').append('<option data-relentity="true" data-relationship="' + n.name + '" data-referencingattributelocalizedname="' + n.referencingattributelocalizedname + '" data-referencingattributename="' + n.referencingattributename + '" data-referencedattributename="' + n.referencedattributename + '"  value="' + n.referencedentityid + '">' + n.referencingattributelocalizedname + '(' + n.referencedentitylocalizedname + ')' + '</option>');
                        }
                    });
                    $('#AddField-recordtype').append('</optgroup>');

                });
            }

            Relationships = data.content;
            loadAttributes($('#AddFieldModal').attr('data-type'));
        });
    }

    function FieldUpDown(direction) {
        var target = $('#pliot-page2').find('.group-row').find('.active');
        var FarTarget = target.parents('.group-row');
        if (direction == 'up') {
            if (FarTarget.prev().length > 0) {
                FarTarget.prev().before(FarTarget.clone(true));
                FarTarget.remove();
            }
        } else {
            if (FarTarget.next().find('.group-null').length <= 0 && FarTarget.next().length > 0) {
                FarTarget.next().after(FarTarget.clone(true));
                FarTarget.remove();
            }
        }
        //重置位置
        ResetPosition();
    }
    function FieldLeftRight(direction) {
        var target = $('#ColumnField').find('.active');
        if (direction == 'left') {
            if (target.prev().length > 0) {
                target.prev().before(target.clone(true));
                target.remove();
            }
        } else {
            if (target.next().hasClass('AddBtn') == false && target.next().length > 0) {
                target.next().after(target.clone(true));
                target.remove();
            }
        }
        //重置位置
        ResetPosition();
    }
    function savePilot() {
        //page1
        savePage1();
        //page2
        Groupings = [];
        Columns = [];
        Filter = '';
        SavePage2();
        CustomReport.Columns = Columns;
        CustomReport.Groupings = Groupings;
        //page3
        SavePage3();
        //page4在page5中保存

        //page5
        var page3 = $('#pliot-page3');
        if (!page3.is(':hidden')) {
            var tablelayId = page3.find('input[name="PilotFormat"]:checked').prop('id');
            if (tablelayId != 'OnlyTable') {
                SavePage5();
                CustomReport.Chart = Chart;
            }
        }
        //page6
        NextPage();
        console.log(222);
        //写进隐藏域
        $('#CustomConfig').val(JSON.stringify(CustomReport));
        $('#QueryConfig').val(JSON.stringify(ReportPage2Entity));
        $('#DefaultFilterConfig').val(JSON.stringify(ReportEntity.Query));
    }

    var MainFilters = [];
    var RelevantFilters = [];
    var Groupings = [];
    var Columns = [];
    var LinkEntities = [];
    //var FiltersConfig = new Xms.Fetch.FilterExpression();

    function savePage1() {
        ReportEntity.Query = [];
        var mainGroup = $('.form-group[data-type="Main"]').children('.andorGroup,.pilot-row').not('[data-relevant=true]');
        var queryMain = new QueryObject();
        queryMain.ColumnSet.AllColumns = true;
        queryMain.Criteria = eachGroup(mainGroup, true);

        var mainReleGroup = $('.form-group[data-type="Main"]').children('.relevant-box');
        if (mainReleGroup.length > 0) {
            queryMain.LinkEntities.push(eachRelevant(mainReleGroup, EntityRelationship));
        }
        ReportEntity.Query.push(queryMain);

        var relevantGroup = $('.form-group[data-type="Relevant"]').children('.andorGroup,.pilot-row');
        var queryRele = new QueryObject();
        queryRele.ColumnSet.AllColumns = true;
        queryRele.Criteria = eachGroup(relevantGroup, true);
        queryRele.EntityName = RelevantEntityName;

        //relevant-LinkEntities
        $('#pliot-page1').find('.form-group[data-type="Relevant"]').find('.pilot-row').each(function (i, n) {
            if ($(n).find('.pilot-fieldM,.pilot-fieldR').find('option:selected').val() != '') {
                var le = new LinkEntity();//生成关联实体别名
                le.Columns.Columns.push($(n).attr('data-attributename').toLowerCase());
                le.LinkToEntityName = RelevantReferencingentityname.toLowerCase();
                le.EntityAlias = RelevantText;
                le.LinkToAttributeName = RelevantEntityName + 'id';
                le.LinkFromEntityName = EntityRelationship;
                le.LinkFromAttributeName = RelevantReferencingattributename.toLowerCase();
                queryRele.LinkEntities.push(le);
            }
        });

        var relevantReleGroup = $('.form-group[data-type="Relevant"]').children('.relevant-box');
        if (relevantReleGroup.length > 0) {
            queryRele.LinkEntities.push(eachRelevant(relevantReleGroup, EntityRelationship));
        }
        ReportEntity.Query.push(queryRele);
    }

    //page2
    function openAddField(type) {
        var target = $('#AddFieldModal');
        if ($('#AddField-recordtype').find('option').length <= 0) {
            loadEntities();
        }
        else {
            loadAttributes(type);
        }
        target.modal('show');
        target.attr('data-type', type);
        target.attr('data-isnew', "true");
        target.find('select').not('#AddField-recordtype').find('option:eq(0)').prop('selected', true);
        target.find('#AddField-width').find('option[value="100"]').prop('selected', true);
        if (type == 'group') {
            target.find('#AddFieldModalLabel').text('添加分组');
            target.find('.isGroup').show();
            $('#AddField-timeBox').hide();
            target.find('.isColumn').hide();
            target.find('.goon-btn').hide();
        }
        else if (type == 'column') {
            target.find('#AddFieldModalLabel').text('添加列');
            target.find('.isGroup').hide();
            target.find('.isColumn').show();
            target.find('.goon-btn').show();
        }
        $('#AddField-recordtype').removeProp('disabled');
        $('#AddField-column').removeProp('disabled');
    }

    function ContinueAddField() {
        var target = $('#AddFieldModal');
        var type = target.attr('data-type');
        if ($('#AddField-recordtype').find('option').length <= 0) {
            loadEntities();
        }
        else {
            loadAttributes(type);
        }
        target.attr('data-type', type);
        target.attr('data-isnew', "true");
        target.find('select').not('#AddField-recordtype').find('option:eq(0)').prop('selected', true);
        target.find('#AddField-width').find('option[value="100"]').prop('selected', true);
        if (type == 'group') {
            target.find('#AddFieldModalLabel').text('添加分组');
            target.find('.isGroup').show();
            $('#AddField-timeBox').hide();
            target.find('.isColumn').hide();
        }
        else if (type == 'column') {
            target.find('#AddFieldModalLabel').text('添加列');
            target.find('.isGroup').hide();
            target.find('.isColumn').show();
        }
        $('#AddField-recordtype').removeProp('disabled');
        $('#AddField-column').removeProp('disabled');
    }

    function insertField(go_on) {
        var target = $('#AddFieldModal');
        //
        var Cname = $('#AddField-column').find('option:selected').val();
        var Crelationship = $('#AddField-column').find('option:selected').attr('data-relationship');
        var Creferencingattributelocalizedname = $('#AddField-column').find('option:selected').attr('data-referencingattributelocalizedname');
        var Centityname = $('#AddField-column').find('option:selected').attr('data-entityname');
        var Centitylocalizedname = $('#AddField-column').find('option:selected').attr('data-entitylocalizedname');
        var Ctype = $('#AddField-column').find('option:selected').attr('data-type');
        var Ctext = $('#AddField-column').find('option:selected').attr('data-text');
        var Cisrel = $('#AddField-column').find('option:selected').attr('data-isrelentity');

        var entityid = $('#AddField-recordtype').find('option:selected').val();
        var Erelationship = $('#AddField-recordtype').find('option:selected').attr('data-relationship');
        var Ereferencingattributelocalizedname = $('#AddField-recordtype').find('option:selected').attr('data-referencingattributelocalizedname');
        var Creferencingattributename = $('#AddField-recordtype').find('option:selected').attr('data-referencingattributename');
        var Creferencedattributename = $('#AddField-recordtype').find('option:selected').attr('data-referencedattributename');

        var time = $('#AddField-time').find('option:selected').val();
        var sort = $('#AddField-sort').find('option:selected').val();
        var width = $('#AddField-width').find('option:selected').val();
        var abstract = $('#AddField-abstract').find('option:selected').attr('data-value');
        var format = $('#AddField-format').prop('checked');
        //
        var extname = '';
        if (entityid.toLowerCase() != EntityId.toLowerCase()) {
            extname = '(' + Ereferencingattributelocalizedname + ')';
            if (entityid.toLowerCase() == RelevantEntityId.toLowerCase()) {
                extname = '(' + RelevantReferencingentitylocalizedname + ')';
            }
        }

        //
        if (target.attr('data-type') == 'group') {
            if (target.attr('data-isnew') == "true") {
                var setTarget = $('#pliot-page2').find('.group-null:first');
                setTarget.removeClass('group-null');
                setTarget.css('border', '');
                setTarget.bind('click', function () {
                    var that = this;
                    ActionShow(that, 'group');
                });
                setTarget.bind('dblclick', function () {
                    EditGroupColumu('group');
                });
            }
            else {
                var setTarget = $('#pliot-page2').find('.group-row').find('.active');
            }
            setTarget.text(Ctext + extname);
            setTarget.parents('.group-row').attr('data-Ctext', Ctext);
            setTarget.parents('.group-row').attr('data-Cname', Cname);
            setTarget.parents('.group-row').attr('data-Crelationship', Crelationship);
            setTarget.parents('.group-row').attr('data-Cisrel', Cisrel);
            setTarget.parents('.group-row').attr('data-Creferencingattributename', Creferencingattributename);
            setTarget.parents('.group-row').attr('data-Creferencedattributename', Creferencedattributename);
            setTarget.parents('.group-row').attr('data-Creferencingattributelocalizedname', Creferencingattributelocalizedname);
            setTarget.parents('.group-row').attr('data-Centityname', Centityname);
            setTarget.parents('.group-row').attr('data-Centitylocalizedname', Centitylocalizedname);
            setTarget.parents('.group-row').attr('data-Ctype', Ctype);

            setTarget.parents('.group-row').attr('data-entityid', entityid);
            setTarget.parents('.group-row').attr('data-Erelationship', Erelationship);
            setTarget.parents('.group-row').attr('data-Ereferencingattributelocalizedname', Ereferencingattributelocalizedname);
            setTarget.parents('.group-row').attr('data-time', time);
            setTarget.parents('.group-row').attr('data-sort', sort);
            setTarget.parents('.group-row').attr('data-width', width);
            setTarget.parents('.group-row').attr('data-abstract', abstract);
            setFirstGroup();
        } else if (target.attr('data-type') == 'column') {
            if (target.attr('data-isnew') == "true") {
                var setTarget = $('#pliot-page2').find('#ColumnField');
                var _html = [];
                _html.push('<th  style="cursor:pointer;"></th>');
                var $html = $(_html.join(''));
                $html.text(Ctext + extname);
                $html.attr('data-Ctext', Ctext);
                $html.attr('data-Cname', Cname);
                $html.attr('data-Crelationship', Crelationship);
                $html.attr('data-Cisrel', Cisrel);
                $html.attr('data-Creferencingattributename', Creferencingattributename);
                $html.attr('data-Creferencedattributename', Creferencedattributename);
                $html.attr('data-Creferencingattributelocalizedname', Creferencingattributelocalizedname);
                $html.attr('data-Centityname', Centityname);
                $html.attr('data-Centitylocalizedname', Centitylocalizedname);
                $html.attr('data-Ctype', Ctype);

                $html.attr('data-entityid', entityid);
                $html.attr('data-Erelationship', Erelationship);
                $html.attr('data-Ereferencingattributelocalizedname', Ereferencingattributelocalizedname);
                $html.attr('data-width', width);
                $html.attr('data-abstract', abstract);
                $html.attr('data-format', format);
                $html.bind('click', function () {
                    var that = this;
                    ActionShow(that, 'column');
                });
                $html.bind('dblclick', function () {
                    EditGroupColumu('column');
                });
                setTarget.find('.AddBtn').before($html);
                setTarget.parent().siblings('tbody').find('tr').prepend('<td>数据</td>');
            }
            else {
                var that = $('#pliot-page2').find('#ColumnField').find('.active');
                that.text(Ctext + extname);
                that.attr('data-Ctext', Ctext);
                that.attr('data-Cname', Cname);
                that.attr('data-Crelationship', Crelationship);
                that.attr('data-Cisrel', Cisrel);
                that.attr('data-Creferencingattributename', Creferencingattributename);
                that.attr('data-Creferencedattributename', Creferencedattributename);
                that.attr('data-Creferencingattributelocalizedname', Creferencingattributelocalizedname);
                that.attr('data-Centityname', Centityname);
                that.attr('data-Centitylocalizedname', Centitylocalizedname);
                that.attr('data-Ctype', Ctype);
                that.attr('data-entityid', entityid);
                that.attr('data-Erelationship', Erelationship);
                that.attr('data-Ereferencingattributelocalizedname', Ereferencingattributelocalizedname);
                that.attr('data-width', width);
                that.attr('data-abstract', abstract);
                that.attr('data-format', format);
            }
        }
        if (!go_on) {
            target.modal('hide');

        }
        else {
            ContinueAddField();
        }
        isEditReport = false;
    }
    function ActionShow(e, type) {
        $(e).css('border', '2px solid #337ab7');
        if (type == 'group') {
            $(e).addClass('active');
            $(e).parents('.group-row').siblings().find('.group-text').not('.group-null').css('border', '').removeClass('active');
        }
        else {
            $(e).addClass('active');
            $(e).siblings('th').css('border', '').removeClass('active');;
        }
    }
    function ResetGroup(e) {
        var that = $(e);

        that.parents('.group-row').removeAttr('data-Cname');
        that.parents('.group-row').removeAttr('data-Crelationship');
        that.parents('.group-row').removeAttr('data-Creferencingattributelocalizedname');
        that.parents('.group-row').removeAttr('data-Centityname');
        that.parents('.group-row').removeAttr('data-Centitylocalizedname');
        that.parents('.group-row').removeAttr('data-entityid');
        that.parents('.group-row').removeAttr('data-Erelationship');
        that.parents('.group-row').removeAttr('data-Ereferencingattributelocalizedname');
        that.parents('.group-row').removeAttr('data-ctype');
        that.parents('.group-row').removeAttr('data-time');
        that.parents('.group-row').removeAttr('data-sort');
        that.parents('.group-row').removeAttr('data-width');
        that.parents('.group-row').removeAttr('data-abstract');
        that.parents('.group-row').removeAttr('data-format');
        that.addClass('group-null').text('&nbsp;');
        that.css('border', '2px dashed #ccc');
        that.removeClass('active');
    }
    function DelGroupColumn(type) {
        if (type == 'group') {
            var that = $('#pliot-page2').find('.group-row').find('.active');
            ResetGroup(that);
            var newGroup = that.parents('.group-row').clone();
            that.parents('.form-group').append(newGroup);
            that.parent('.group-row').remove();
            ResetPosition();
            setFirstGroup();
        }
        else if (type == 'column') {
            var that = $('#pliot-page2').find('#ColumnField').find('.active');
            that.parents('.form-group').find('tbody tr').each(function (i, n) {
                $(n).find('td:eq(0)').remove();
            });
            that.remove();
        }
        isEditReport = false;
    }
    function setFirstGroup() {
        var setTarget = $('#pliot-page2').find('.group-null:first');
        setTarget.html('<span style="cursor:pointer;">单击此处添加分组</span>');
    }
    function ResetPosition() {
        $('#pliot-page2').find('.group-row').each(function (i, n) {
            $(n).find('.placeholder').removeClass().addClass('col-sm-' + (i + 1)).addClass('placeholder');
        });
    }
    function EditGroupColumu(type) {
        var target = $('#AddFieldModal');
        target.attr('data-type', type);
        target.attr('data-isnew', "false");
        if (type == 'group') {
            var that = $('#pliot-page2').find('.group-row').find('.active');
            if (that.length == 0) {
                return false;
            }
            $('#AddField-recordtype').find('option[value="' + that.parents('.group-row').attr('data-entityid') + '"]').prop('selected', true);
            $('#AddField-recordtype').prop('disabled', 'disabled');
            var _html = [];
            _html.push('<option data-text="' + that.parents('.group-row').attr('data-ctext') + '" data-relationship="' + that.parents('.group-row').attr('data-crelationship') + '" data-referencingattributelocalizedname="' + that.parents('.group-row').attr('data-creferencingattributelocalizedname') + '" data-type="' + that.parents('.group-row').attr('data-ctype') + '" value="' + that.parents('.group-row').attr('data-cname') + '" data-entityname="' + that.parents('.group-row').attr('data-centityname') + '" data-entitylocalizedname="' + that.parents('.group-row').attr('data-centitylocalizedname') + '">' + that.parents('.group-row').attr('data-ctext') + '</option>');
            $('#AddField-column').html(_html.join(''));
            $('#AddField-column').prop('disabled', 'disabled');

            $('#AddField-time').find('option[value="' + that.parents('.group-row').attr('data-time') + '"]').prop('selected', true);
            $('#AddField-type').text(that.parents('.group-row').attr('data-ctype'));
            if (that.parents('.group-row').attr('data-ctype').toLowerCase() == 'datetime') {
                $('#AddField-timeBox').show();
            }
            else {
                $('#AddField-timeBox').hide();
            }
            SummaryValue(that.parents('.group-row').attr('data-ctype'));
            $('#AddField-sort').find('option[value="' + that.parents('.group-row').attr('data-sort') + '"]').prop('selected', true);
            $('#AddField-width').find('option[value="' + that.parents('.group-row').attr('data-width') + '"]').prop('selected', true);
            $('#AddField-abstract').find('option[data-value="' + that.parents('.group-row').attr('data-abstract') + '"]').prop('selected', true);

            target.find('#AddFieldModalLabel').text('编辑分组');
            target.find('.isGroup').show();
            target.find('.isColumn').hide();
        }
        else if (type == 'column') {
            var that = $('#pliot-page2').find('#ColumnField').find('.active');
            if (that.length == 0) {
                return false;
            }
            $('#AddField-recordtype').find('option[value="' + that.attr('data-entityid') + '"]').prop('selected', true);
            $('#AddField-recordtype').prop('disabled', 'disabled');
            var _html = [];
            _html.push('<option data-text="' + that.attr('data-ctext') + '"  data-relationship="' + that.attr('data-crelationship') + '" data-referencingattributelocalizedname="' + that.attr('data-creferencingattributelocalizedname') + '" data-type="' + that.attr('data-ctype') + '" value="' + that.attr('data-cname') + '" data-entityname="' + that.attr('data-centityname') + '" data-entitylocalizedname="' + that.attr('data-centitylocalizedname') + '">' + that.attr('data-ctext') + '</option>')
            $('#AddField-column').html(_html.join(''));
            $('#AddField-column').prop('disabled', 'disabled');

            $('#AddField-type').text(that.attr('data-ctype'));
            SummaryValue(that.attr('data-ctype'));
            $('#AddField-width').find('option[value="' + that.attr('data-width') + '"]').prop('selected', true);
            $('#AddField-abstract').find('option[data-value="' + that.attr('data-abstract') + '"]').prop('selected', true);
            if (that.attr('data-format') == 'true') {
                $('#AddField-format').prop('checked', true);
            }
            else {
                $('#AddField-format').prop('checked', false);
            }

            target.find('#AddFieldModalLabel').text('编辑列');
            target.find('.isGroup').hide();
            target.find('.isColumn').show();
        }
        target.modal('show');
    }
    function SavePage2() {
        var target = $('#pliot-page2');
        var queryObj = new QueryObject();
        var relLinkEntities = new Array();
        $('#GroupList').find('.group-row').each(function (i, n) {
            if ($(n).find('.group-null').length <= 0) {
                var columnname = $(n).attr('data-cname');
                var relationship = $(n).attr('data-crelationship').toLowerCase();
                var time = $(n).attr('data-time');
                if ($(n).attr('data-ctype') != 'datetime') {
                    time = '';
                }
                var sort = $(n).attr('data-sort');
                var width = $(n).attr('data-width');
                var abstract = $(n).attr('data-abstract');
                var centityname = $(n).attr('data-centityname').toLowerCase();
                var isrelentity = $(n).attr('data-Cisrel');
                var referencingattributename = $(n).attr('data-creferencingattributename').toLowerCase();
                var referencedattributename = $(n).attr('data-creferencedattributename').toLowerCase();
                var field = '';
                if (relationship == '') {
                    field = columnname;
                } else {
                    field = relationship + '.' + columnname;
                }
                var Grouping = {
                    Field: field,
                    Sort: sort,
                    Width: width,
                    DateGrouping: time == '' ? null : time,
                    SummaryValue: abstract == '' ? null : abstract
                };
                Groupings.push(Grouping);
                //
                if (centityname != EntityRelationship.toLowerCase()) {
                    var le = new LinkEntity();//生成关联实体别名
                    le.Columns.Columns.push(columnname);
                    le.LinkToEntityName = centityname;
                    le.EntityAlias = relationship;
                    le.LinkToAttributeName = centityname + 'id';
                    le.LinkFromEntityName = EntityRelationship;
                    if (relationship == RelevantEntityName.toLowerCase()) {
                        le.LinkFromAttributeName = RelevantReferencingattributename.toLowerCase();
                        le.LinkToAttributeName = RelevantReferencedattributename.toLowerCase();
                    }
                    else {
                        le.LinkFromAttributeName = referencingattributename;
                        le.LinkToAttributeName = referencedattributename;
                    }

                    if (isrelentity == 'false' || RelevantEntityName.toLowerCase() == relationship) {
                        var ishaslink = false;
                        for (var i = 0; i < queryObj.LinkEntities.length; i++) {
                            if (queryObj.LinkEntities[i].EntityAlias.toLowerCase() == relationship) {
                                queryObj.LinkEntities[i].Columns.Columns.push(columnname);
                                ishaslink = true;
                                break;
                            }
                        }
                        if (!ishaslink) {
                            queryObj.LinkEntities.push(le);
                        }
                    } else {
                        if (RelevantEntityName.toLowerCase() != relationship) {
                            var ishaslink = false;
                            for (var i = 0; i < relLinkEntities.length; i++) {
                                if (relLinkEntities[i].EntityAlias.toLowerCase() == relationship) {
                                    relLinkEntities[i].Columns.Columns.push(columnname);
                                    ishaslink = true;
                                    break;
                                }
                            }
                            if (!ishaslink) {
                                le.FromEntityAlias = RelevantEntityName;
                                le.LinkFromEntityName = RelevantReferencingentityname;
                                relLinkEntities.push(le);
                            }
                        }
                    }
                }
                else {
                    if ($.inArray(columnname, queryObj.ColumnSet.Columns) < 0) {//列名不存在
                        queryObj.ColumnSet.Columns.push(columnname);
                    }
                }
            }
        });
        $('#ColumnField').find('th').each(function (i, n) {
            if ($(n).hasClass('AddBtn') == false) {
                var columnname = $(n).attr('data-cname');
                var relationship = $(n).attr('data-crelationship').toLowerCase();
                var width = $(n).attr('data-width');
                var abstract = $(n).attr('data-abstract') == '' ? null : $(n).attr('data-abstract');
                var format = $(n).attr('data-format');
                var centityname = $(n).attr('data-centityname').toLowerCase();
                var isrelentity = $(n).attr('data-Cisrel');
                var referencingattributename = $(n).attr('data-creferencingattributename').toLowerCase();
                var referencedattributename = $(n).attr('data-creferencedattributename').toLowerCase();
                var field = '';
                if (relationship == '') {
                    field = columnname;
                } else {
                    field = relationship + '.' + columnname;
                }
                var Column = {
                    ID: '',
                    Field: field,
                    Width: width,
                    SummaryValue: abstract == '' ? null : abstract,
                    Sort: null
                };
                if ($('#PilotSort-Sort').find('option:selected').val().toLowerCase() == field.toLowerCase()) {
                    Column.Sort = $('#PilotSortModal').find('input[name="PilotSort-SortType"][checked=checked]').val();
                }
                Columns.push(Column);

                //
                if (centityname != EntityRelationship.toLowerCase() || (typeof $(n).attr('data-crelationship') != 'undefined' && $(n).attr('data-crelationship') != '')) {
                    var le = new LinkEntity();//生成关联实体别名
                    le.Columns.Columns.push(columnname);
                    le.LinkToEntityName = centityname;
                    le.EntityAlias = relationship;
                    le.LinkToAttributeName = centityname + 'id';
                    le.LinkFromEntityName = EntityRelationship;
                    le.FromEntityAlias = EntityRelationship;
                    if (relationship == RelevantEntityName.toLowerCase()) {
                        le.LinkFromAttributeName = RelevantReferencingattributename.toLowerCase();
                        le.LinkToAttributeName = RelevantReferencedattributename.toLowerCase();
                    }
                    else {
                        le.LinkFromAttributeName = referencingattributename;
                        le.LinkToAttributeName = referencedattributename;
                    }

                    if (isrelentity == 'false' || RelevantEntityName.toLowerCase() == relationship) {
                        var ishaslink = false;
                        for (var i = 0; i < queryObj.LinkEntities.length; i++) {
                            if (queryObj.LinkEntities[i].EntityAlias.toLowerCase() == relationship) {
                                queryObj.LinkEntities[i].Columns.Columns.push(columnname);
                                ishaslink = true;
                                break;
                            }
                        }
                        if (!ishaslink) {
                            queryObj.LinkEntities.push(le);
                        }
                    } else {
                        if (RelevantEntityName.toLowerCase() != relationship) {
                            var ishaslink = false;
                            for (var i = 0; i < relLinkEntities.length; i++) {
                                if (relLinkEntities[i].EntityAlias.toLowerCase() == relationship) {
                                    relLinkEntities[i].Columns.Columns.push(columnname);
                                    ishaslink = true;
                                    break;
                                }
                            }
                            if (!ishaslink) {
                                le.FromEntityAlias = RelevantEntityName;
                                le.LinkFromEntityName = RelevantReferencingentityname;
                                relLinkEntities.push(le);
                            }
                        }
                    }
                }
                else {
                    if ($.inArray(columnname, queryObj.ColumnSet.Columns) < 0) {//列名不存在
                        queryObj.ColumnSet.Columns.push(columnname);
                    }
                }
            }
        });
        if (relLinkEntities.length > 0) {
            var ishasrellink = false;
            for (var i = 0; i < queryObj.LinkEntities.length; i++) {
                if (queryObj.LinkEntities[i].EntityAlias.toLowerCase() == RelevantEntityName.toLowerCase()) {
                    queryObj.LinkEntities[i].LinkEntities = null;
                    queryObj.LinkEntities[i].LinkEntities = relLinkEntities;
                    ishasrellink = true;
                    break;
                }
            }
            if (!ishasrellink) {
                var le = new LinkEntity();//生成关联实体别名
                le.LinkToEntityName = EntityRelationship.toLowerCase();
                le.EntityAlias = RelevantEntityName.toLowerCase();
                le.LinkToAttributeName = RelevantReferencedentityname.toLowerCase() + 'id';
                le.LinkFromEntityName = RelevantEntityName.toLowerCase();
                le.LinkFromAttributeName = RelevantReferencingattributename.toLowerCase();
                le.LinkEntities = null;
                le.LinkEntities = relLinkEntities;
                queryObj.LinkEntities.push(le);
            }
        }

        //
        //
        var setrowField = $('#SetRow-Field').find('option:selected').val();
        var setrowDirection = $('#SetRow-direction').find('option:selected').val();
        var setrowRowNum = $('#SetRow-rownum').val();
        if ($('#SetRow-Field').find('option').length > 0) {
            var Filter = {
                Filed: setrowField,
                Operator: setrowDirection,
                Value: setrowRowNum
            };
            CustomReport.Filter = Filter;
            //TableLayout.push(Filter);
        }
        CustomReport.Query = queryObj;
        ReportPage2Entity.Query = queryObj;
    }
    function OpenPilotSort() {
        var _html = [];
        $('#ColumnField').find('th').not('.AddBtn').each(function (i, n) {
            var columnname = $(n).attr('data-cname');
            var relationship = $(n).attr('data-crelationship');
            var fieldname = $(n).text();
            var field = '';
            if (relationship == '') {
                field = columnname;
            } else {
                field = relationship + '.' + columnname;
            }
            _html.push('<option value="' + field + '">' + fieldname + '</option>');
        });
        if (_html.length == 0) {
            Xms.Web.Alert(true, '请至少添加一个列');
            return false;
        }
        $('#PilotSort-Sort').html(_html.join(''));
        $('#PilotSortModal').modal('show');
    }
    function OpenSetRow() {
        var target = $('#SetRowModal');
        var _html = [];
        $('#ColumnField').find('th').not('.AddBtn').each(function (i, n) {
            if ($(n).attr('data-abstract') != '') {
                var columnname = $(n).attr('data-cname');
                var relationship = $(n).attr('data-crelationship');
                var fieldname = $(n).attr('data-centitylocalizedname');
                var columntext = $(n).text();
                var field = '';
                if (relationship == '') {
                    field = columnname;
                } else {
                    field = relationship + '.' + columnname;
                }
                _html.push('<option value="' + field + '">' + columntext + '</option>');
            }
        });
        if (_html.length == 0) {
            Xms.Web.Alert(true, '请至少添加一个具有汇总的列');
            return false;
        }
        $('#SetRow-Field').html(_html.join(''));
        target.modal('show');
    }
    function SavePilotSort() {
        var target = $('#PilotSortModal');
        target.modal('hide');
    }
    function SaveSetRow() {
        var target = $('#SetRowModal');
        target.modal('hide');
    }
    //page3/4/5
    function SavePage3() {
        var target = $('#pliot-page3');
        var pilotFormat = target.find('input[name="PilotFormat"]:checked').val();
        CustomReport.TableLayout = pilotFormat;
    }
    function SavePage4() {
        var target = $('#pliot-page4');
        var pilotChartType = target.find('input[name="PilotChartType"]:checked').val();
    }
    function OpenPage5() {
        var groupHtml = [];
        var columeHtml = [];
        var intHtml = [];
        $('#GroupList').find('.group-row').each(function (i, n) {
            if ($(n).find('.group-null').length <= 0) {
                var GroupOption = '<option data-abstract="' + $(n).attr('data-abstract') + '" value="' + $(n).attr('data-cname') + '" data-relationship="' + $(n).attr('data-crelationship') + '" data-referencingattributelocalizedname="' + $(n).attr('data-creferencingattributelocalizedname') + '" data-entityname="' + $(n).attr('data-centityname') + '" data-entitylocalizedname="' + $(n).attr('data-centitylocalizedname') + '" data-type="' + $(n).attr('data-ctype') + '" data-entityid="' + $(n).attr('data-entityid') + '" data-erelationship="' + $(n).attr('data-erelationship') + '" data-ereferencingattributelocalizedname="' + $(n).attr('data-ereferencingattributelocalizedname') + '" >' + $(n).find('.group-text').text() + '</option>';
                groupHtml.push(GroupOption);
            }
        });
        $('#ColumnField').find('th').not('.AddBtn').each(function (i, n) {
            var columeOption = '<option data-abstract="' + $(n).attr('data-abstract') + '" value="' + $(n).attr('data-cname') + '" data-relationship="' + $(n).attr('data-crelationship') + '" data-referencingattributelocalizedname="' + $(n).attr('data-creferencingattributelocalizedname') + '" data-entityname="' + $(n).attr('data-centityname') + '" data-entitylocalizedname="' + $(n).attr('data-centitylocalizedname') + '" data-type="' + $(n).attr('data-ctype') + '" data-entityid="' + $(n).attr('data-entityid') + '" data-erelationship="' + $(n).attr('data-erelationship') + '" data-ereferencingattributelocalizedname="' + $(n).attr('data-ereferencingattributelocalizedname') + '" >' + $(n).text() + '</option>';
            columeHtml.push(columeOption);
            if ($.inArray($(n).attr('data-ctype'), inttype) != -1 && $(n).attr('data-abstract') != '') {
                intHtml.push(columeOption);
            }
        });
        if ($('#pliot-page4').find('input[name="PilotChartType"]:checked').val() == 'Pie') {
            if (groupHtml.length > 0) {
                $('#Chart-Section').html(groupHtml.join(''));
            } else {
                $('#Chart-Section').html(columeHtml.join(''));
            }
            $('#Chart-Value').html(intHtml.join(''));
            var oldSection = $('#Chart-Section').find('option[data-entityid="' + $('#Chart-Section').attr('data-entityid') + '"][value="' + $('#Chart-Section').attr('data-name') + '"]');
            if (oldSection.length == 0) {
                $('#Chart-Section').change();
            } else {
                oldSection.prop('selected', true);
            }
            var oldValue = $('#Chart-Value').find('option[data-entityid="' + $('#Chart-Value').attr('data-entityid') + '"][value="' + $('#Chart-Value').attr('data-name') + '"]');
            if (oldValue.length == 0) {
                $('#Chart-Value').change();
            } else {
                oldValue.prop('selected', true);
            }
            $('.CakeChart').show();
            $('.OtherChart').hide();
        } else {
            if (groupHtml.length > 0) {
                $('#Chart-Field').html(groupHtml.join(''));
            } else {
                $('#Chart-Field').html(columeHtml.join(''));
            }
            $('#Chart-FirstField').html(intHtml.join(''));
            $('#Chart-SecField').html('<option value="">--请选择--</option>');
            $('#Chart-SecField').append(intHtml.join(''));
            var oldField = $('#Chart-Field').find('option[data-entityid="' + $('#Chart-Field').attr('data-entityid') + '"][value="' + $('#Chart-Field').attr('data-name') + '"]');
            if (oldField.length == 0) {
                $('#Chart-Field').change();
            } else {
                oldField.prop('selected', true);
            }
            var oldFirdtField = $('#Chart-FirstField').find('option[data-entityid="' + $('#Chart-FirstField').attr('data-entityid') + '"][value="' + $('#Chart-FirstField').attr('data-name') + '"]');
            if (oldFirdtField.length == 0) {
                $('#Chart-FirstField').change();
            } else {
                oldFirdtField.prop('selected', true);
            }
            var oldSecField = $('#Chart-SecField').find('option[data-entityid="' + $('#Chart-SecField').attr('data-entityid') + '"][value="' + $('#Chart-SecField').attr('data-name') + '"]');
            if (oldSecField.length == 0) {
                $('#Chart-SecField').change();
            } else {
                oldSecField.prop('selected', true);
            }
            $('.CakeChart').hide();
            $('.OtherChart').show();
        }
        if (isEditReport) {
            EditPage5();
        }
    }
    function PrevPage() {
        var target = $('#PilotModal');
        $('#NextBtn').find('.NextText').text('下一步');
        //$('#NextBtn').unbind();
        //$('#NextBtn').bind('click', function () {
        //    NextPage();
        //});
        var datapage = target.find('.modal-body:visible').attr('data-page');
        var showTarget = target.find('.modal-body[data-page="' + (parseInt(datapage) - 1) + '"]');
        if (showTarget.length > 0) {
            target.find('.modal-body:visible').hide();
            showTarget.show();
            if ((parseInt(datapage) - 1) == 5) {
                OpenPage5();
            }
        }
    }
    function NextPage() {
        if ($('#pliot-page1').find('label[name=digitsTip]').length > 0) {
            Xms.Web.Alert(false, '填写的内容有误，请检查');
            return false;
        }
        var target = $('#PilotModal')
        var datapage = target.find('.modal-body:visible').attr('data-page');
        var showTarget = target.find('.modal-body[data-page="' + (parseInt(datapage) + 1) + '"]');
        //进入第三页前，第二页至少选择一个列
        if ((parseInt(datapage) + 1) == 3) {
            if (OpenPage3() == false) {
                return false;
            } else {
                $('#pliot-page3').find('input[name="PilotFormat"]').change();
            }
        }
        //进入第五页前,按钮变完成
        if ((parseInt(datapage) + 1) == 5) {
            $('#NextBtn').find('.NextText').text('完成');
            //$('#NextBtn').unbind();
            //$('#NextBtn').bind('click', function () {
            //    savePilot();
            //});
        }

        //如果第三页选择仅表格   或者  到达第五页 则保存隐藏
        if (((parseInt(datapage) + 1) > 3 && $('#pliot-page3').find('input[name="PilotFormat"]:checked').prop('id') == 'OnlyTable') || (parseInt(datapage) + 1) > 5) {
            target.modal('hide');
            return true;
        }
        if (showTarget.length > 0) {
            target.find('.modal-body:visible').hide();
            showTarget.show();
            if ((parseInt(datapage) + 1) == 5) {
                OpenPage5();
            }
        }
    }
    function OpenPage3() {
        if ($('#ColumnField').find('th').not('.AddBtn').length == 0) {
            Xms.Web.Alert(true, '若要继续，必须至少选择一个要加入报表的列。');
            return false;
        }
        var hasInt = false;
        $('#ColumnField').find('th').not('.AddBtn').each(function (i, n) {
            if ($.inArray($(n).attr('data-ctype'), inttype) != -1 && $(n).attr('data-abstract') != '') {
                $('#page3-tips').hide();//如果存在数值类型字段，则提示隐藏
                $('#TableChart').removeProp('disabled');
                $('#TableInChart').removeProp('disabled');
                hasInt = true;
                return false;
            }
        });
        if (hasInt == false) {
            $('#page3-tips').show();
            $('#OnlyTable').prop('checked', true);
            $('#TableChart').prop('disabled', 'disabled');
            $('#TableInChart').prop('disabled', 'disabled');
        }
        return true;
    }
    function SavePage5() {
        Chart = { Type: '', Legend: true, DataLabels: true, ValueAxes: new Array(), ColumnAxis: { Field: '', Name: '' } };
        var dataLabel = $('#Chart-isShowLabel').prop('checked');
        var isShowText = $('#Chart-isShowText').prop('checked');
        if ($('#pliot-page4').find('input[name="PilotChartType"]:checked').val() == 'Pie') {
            var ChartSectionName = $('#Chart-Section').find('option:selected').val();
            var ChartSectionEntity = $('#Chart-Section').find('option:selected').attr('data-entityname');
            var ChartSectionText = $('#Chart-Section').find('option:selected').text();
            var ChartSectionAbs = $('#Chart-Section').find('option:selected').attr('data-abstract');
            var ChartSectionLabel = $('#Chart-Section').val();
            //
            var ChartValueName = $('#Chart-Value').find('option:selected').val();
            var ChartValueEntity = $('#Chart-Value').find('option:selected').attr('data-entityname');
            var ChartValueText = $('#Chart-Value').find('option:selected').text();
            var ChartValueAbs = $('#Chart-Value').find('option:selected').attr('data-abstract');
            var ChartValueLabel = $('#Chart-Value').val();
            var axisField = '';
            if (typeof ChartValueName != 'undefined') {
                if (ChartValueEntity == EntityRelationship) {
                    axisField = ChartValueName.toLowerCase();
                } else {
                    axisField = ChartValueEntity + '_' + ChartValueName.toLowerCase();
                }
            }
            var ValueAxis = {
                Field: axisField,
                Name: ChartValueLabel,
                SummaryValue: ChartValueAbs == '' ? null : ChartValueAbs
            };
            Chart.ValueAxes.push(ValueAxis);
            var secaxisField = '';
            if (typeof ChartSectionName != 'undefined') {
                if (ChartSectionEntity == EntityRelationship) {
                    secaxisField = ChartSectionName
                } else {
                    secaxisField = ChartSectionEntity + '_' + ChartSectionName.toLowerCase();
                }
            }
            Chart.ColumnAxis.Field = secaxisField;
            Chart.ColumnAxis.Name = ChartSectionLabel;
        }
        else {
            var ChartFieldName = $('#Chart-Field').find('option:selected').val();
            var ChartFieldEntity = $('#Chart-Field').find('option:selected').attr('data-entityname');
            var ChartFieldText = $('#Chart-Field').find('option:selected').text();
            var ChartFieldAbs = $('#Chart-Field').find('option:selected').attr('data-abstract');
            var ChartFieldLabel = $('#Chart-Label').val();
            //
            var ChartFirstFieldName = $('#Chart-FirstField').find('option:selected').val();
            var ChartFirstFieldEntity = $('#Chart-FirstField').find('option:selected').attr('data-entityname');
            var ChartFirstFieldText = $('#Chart-FirstField').find('option:selected').text();
            var ChartFirstFieldAbs = $('#Chart-FirstField').find('option:selected').attr('data-abstract');
            var ChartFirstLabel = $('#Chart-FirstLabel').val();
            //
            var ChartSecFieldName = $('#Chart-SecField').find('option:selected').val();
            var ChartSecFieldEntity = $('#Chart-SecField').find('option:selected').attr('data-entityname');
            var ChartSecFieldText = $('#Chart-SecField').find('option:selected').text();
            var ChartSecFieldAbs = $('#Chart-SecField').find('option:selected').attr('data-abstract');
            var ChartSecLabel = $('#Chart-SecLabel').val();
            var firaxisField = '';
            if (typeof ChartFirstFieldName != 'undefined') {
                if (ChartFirstFieldEntity == EntityRelationship) {
                    firaxisField = ChartFirstFieldName.toLowerCase();
                } else {
                    firaxisField = ChartFirstFieldEntity + '_' + ChartFirstFieldName.toLowerCase();
                }
            }
            var FirstValueAxis = {
                Field: firaxisField,
                Name: ChartFirstLabel,
                SummaryValue: ChartFirstFieldAbs == '' ? null : ChartFirstFieldAbs
            };
            Chart.ValueAxes.push(FirstValueAxis);
            var secaxisField = '';
            if (typeof ChartSecFieldName != 'undefined') {
                if (ChartSecFieldEntity == EntityRelationship) {
                    secaxisField = ChartSecFieldName.toLowerCase();
                } else {
                    secaxisField = ChartSecFieldEntity + '_' + ChartSecFieldName.toLowerCase();
                }
            }
            if (ChartSecFieldName != '') {
                var SecValueAxis = {
                    Field: secaxisField,
                    Name: ChartSecLabel,
                    SummaryValue: ChartSecFieldAbs == '' ? null : ChartSecFieldAbs
                };
                Chart.ValueAxes.push(SecValueAxis);
            }
            var axisField = '';
            if (typeof ChartFieldName != 'undefined') {
                if (ChartFieldEntity == EntityRelationship) {
                    axisField = ChartFieldName.toLowerCase();
                } else {
                    axisField = ChartFieldEntity + '_' + ChartFieldName.toLowerCase();
                }
            }
            Chart.ColumnAxis.Field = axisField;
            Chart.ColumnAxis.Name = ChartFieldLabel;
        }
        Chart.Type = $('#pliot-page4').find('input[name="PilotChartType"]:checked').val();
        Chart.DataLabels = dataLabel;
        Chart.Legend = isShowText;

    }
    function HidePilotModal() {
        $('#PilotModal').modal('hide');
    }
    function HideSetRowModal() {
        $('#SetRowModal').modal('hide');
    }
    function HidePilotSortModal() {
        $('#PilotSortModal').modal('hide');
    }
    function CancelPilot() {
        $('#PilotModal').off('hidden.bs.modal').on('hidden.bs.modal', function () {
            $('#PilotModal').parent('div').remove();
        })
        Xms.Web.Alert(true, '是否要关闭此向导？您所做的更改将不保存', function () {
            $('#CustomConfig').val('');
            $('#QueryConfig').val('');
            $('#DefaultFilterConfig').val('');
            $('#PilotModal').modal('hide');
        }, null);
    }
    function LinkEntity() {
        var self = new Object();
        self.LinkFromEntityName = '';
        self.LinkFromAttributeName = '';
        self.LinkToEntityName = '';
        self.LinkToAttributeName = '';
        self.JoinOperator = 1;
        self.EntityAlias = '';
        self.FromEntityAlias = '';
        self.Columns = { Columns: [] };
        self.LinkEntities = null;
        self.FilterExpression = null;
        return self;
    }
    function FilterExpression() {
        var self = new Object();
        self.FilterOperator = 'and';
        self.Conditions = new Array();
        self.Filters = new Array();
        return self;
    }
    function QueryObject() {
        var self = new Object();
        self.Distinct = false;
        self.NoLock = true;
        self.EntityName = EntityRelationship.toLowerCase();
        self.Criteria = new Object();
        self.Orders = new Array();
        self.ColumnSet = { AllColumns: false, Columns: new Array() };
        self.LinkEntities = new Array()
        return self;
    }

    function EditInitPage2() {

    }
</script>